<template>
  <div class="note-detail">
    <!-- 顶部导航 -->
    <div class="header">
      <button class="back-btn" @click="goBack">
        <ArrowLeftIcon class="icon" />
      </button>
      <h1 class="title">笔记详情</h1>
      <button class="more-btn">
        <MoreHorizontalIcon class="icon" />
      </button>
    </div>

    <!-- 笔记内容 -->
    <div v-if="isLoading" class="loading">
      <div class="spinner"></div>
    </div>
    
    <div v-else-if="note" class="note-content">
      <!-- 用户信息 -->
      <div class="user-info">
        <img :src="note.user.avatar" alt="用户头像" class="avatar">
        <div class="user-details">
          <div class="username">{{ note.user.name }}</div>
          <div class="post-time">{{ formatDate(note.createdAt) }}</div>
        </div>
        <button class="follow-btn" v-if="!isCurrentUser">关注</button>
      </div>
      
      <!-- 笔记标题和内容 -->
      <h2 class="note-title">{{ note.title }}</h2>
      <div class="note-text" v-html="note.content"></div>
      
      <!-- 媒体内容 -->
      <div class="media-grid">
        <div 
          v-for="(media, index) in note.media" 
          :key="index"
          class="media-item"
        >
          <img v-if="media.type === 'image'" :src="media.url" alt="笔记图片" class="media-img">
          <video v-else-if="media.type === 'video'" :src="media.url" controls class="media-video"></video>
        </div>
      </div>
      
      <!-- 标签 -->
      <div class="tags">
        <span v-for="(tag, index) in note.tags" :key="index" class="tag">#{{ tag }}</span>
      </div>
      
      <!-- 互动统计 -->
      <div class="stats">
        <div class="stat-item">
          <EyeIcon class="stat-icon" />
          <span>{{ note.views }}</span>
        </div>
        <div class="stat-item">
          <HeartIcon class="stat-icon" />
          <span>{{ note.likes }}</span>
        </div>
        <div class="stat-item">
          <MessageCircleIcon class="stat-icon" />
          <span>{{ note.comments }}</span>
        </div>
      </div>
      
      <!-- 互动按钮 -->
      <div class="action-buttons">
        <button 
          class="action-btn"
          :class="{ 'active': note.isLiked }"
          @click="toggleLike"
        >
          <HeartIcon class="btn-icon" :fill="note.isLiked ? 'currentColor' : 'none'" />
          <span>点赞</span>
        </button>
        <button class="action-btn" @click="focusCommentInput">
          <MessageCircleIcon class="btn-icon" />
          <span>评论</span>
        </button>
        <button 
          class="action-btn"
          :class="{ 'active': note.isCollected }"
          @click="toggleCollect"
        >
          <BookmarkIcon class="btn-icon" :fill="note.isCollected ? 'currentColor' : 'none'" />
          <span>收藏</span>
        </button>
        <button class="action-btn">
          <Share2Icon class="btn-icon" />
          <span>分享</span>
        </button>
      </div>
      
      <!-- 评论列表 -->
      <div class="comments-section">
        <h3 class="section-title">评论 ({{ note.comments }})</h3>
        
        <div v-if="commentsLoading" class="loading-comments">
          <div class="small-spinner"></div>
        </div>
        
        <div v-else-if="comments.length === 0" class="no-comments">
          暂无评论，快来发表第一条评论吧
        </div>
        
        <div v-else class="comments-list">
          <div v-for="comment in comments" :key="comment.id" class="comment-item">
            <img :src="comment.user.avatar" alt="用户头像" class="comment-avatar">
            <div class="comment-content">
              <div class="comment-header">
                <span class="comment-user">{{ comment.user.name }}</span>
                <span class="comment-time">{{ formatDate(comment.createdAt) }}</span>
              </div>
              <p class="comment-text">{{ comment.content }}</p>
              <div class="comment-actions">
                <button class="like-btn">
                  <HeartIcon class="like-icon" />
                  <span>12</span>
                </button>
                <button class="reply-btn">回复</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 评论输入框 -->
    <div class="comment-input-container">
      <input 
        type="text" 
        placeholder="写评论..." 
        class="comment-input"
        ref="commentInput"
      >
      <button class="send-btn">发送</button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useNoteStore } from '@/store/notes'
import { useCommentStore } from '@/store/comments'
import { useAuthStore } from '@/store/auth'
import { formatDate } from '@/utils/date'
import { 
  ArrowLeftIcon, MoreHorizontalIcon, EyeIcon, 
  HeartIcon, MessageCircleIcon, BookmarkIcon, 
  Share2Icon 
} from 'lucide-vue-next'

const route = useRoute()
const router = useRouter()
const noteStore = useNoteStore()
const commentStore = useCommentStore()
const authStore = useAuthStore()

const noteId = route.params.id
const note = computed(() => noteStore.currentNote)
const isLoading = computed(() => noteStore.isLoading)
const comments = computed(() => commentStore.comments)
const commentsLoading = computed(() => commentStore.isLoading)
const commentInput = ref(null)

const isCurrentUser = computed(() => {
  return authStore.user && authStore.user.id === note.value?.user?.id
})

onMounted(() => {
  noteStore.fetchNoteById(noteId)
  commentStore.fetchCommentsByNoteId(noteId)
})

const goBack = () => {
  router.go(-1)
}

const toggleLike = () => {
  if (!authStore.isAuthenticated) {
    router.push('/login')
    return
  }
  noteStore.toggleLike(noteId)
}

const toggleCollect = () => {
  if (!authStore.isAuthenticated) {
    router.push('/login')
    return
  }
  noteStore.toggleCollect(noteId)
}

const focusCommentInput = () => {
  commentInput.value.focus()
}
</script>

<style scoped>
.note-detail {
  padding-bottom: 60px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background-color: white;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn, .more-btn {
  background: none;
  border: none;
  cursor: pointer;
}

.icon {
  width: 24px;
  height: 24px;
}

.title {
  font-size: 18px;
  font-weight: bold;
  margin: 0;
}

.loading {
  display: flex;
  justify-content: center;
  padding: 32px 0;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 194, 255, 0.3);
  border-radius: 50%;
  border-top-color: var(--color-primary);
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.note-content {
  padding: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  margin-right: 12px;
}

.user-details {
  flex: 1;
}

.username {
  font-weight: bold;
  margin-bottom: 4px;
}

.post-time {
  font-size: 12px;
  color: #888;
}

.follow-btn {
  padding: 6px 16px;
  background-color: var(--color-primary);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
}

.note-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 12px;
}

.note-text {
  line-height: 1.6;
  margin-bottom: 16px;
}

.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.media-item {
  border-radius: 8px;
  overflow: hidden;
}

.media-img, .media-video {
  width: 100%;
  height: 150px;
  object-fit: cover;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tag {
  padding: 4px 12px;
  background-color: #f0f9ff;
  color: var(--color-primary);
  border-radius: 20px;
  font-size: 14px;
}

.stats {
  display: flex;
  gap: 24px;
  padding: 12px 0;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  margin-bottom: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #666;
}

.stat-icon {
  width: 16px;
  height: 16px;
}

.action-buttons {
  display: flex;
  justify-content: space-around;
  margin-bottom: 24px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: none;
  border: none;
  cursor: pointer;
  color: #666;
}

.action-btn.active {
  color: var(--color-primary);
}

.btn-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 4px;
}

.comments-section {
  margin-bottom: 60px;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 16px;
}

.loading-comments {
  display: flex;
  justify-content: center;
  padding: 16px 0;
}

.small-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(0, 194, 255, 0.3);
  border-radius: 50%;
  border-top-color: var(--color-primary);
  animation: spin 1s linear infinite;
}

.no-comments {
  text-align: center;
  padding: 24px 0;
  color: #888;
}

.comment-item {
  display: flex;
  margin-bottom: 16px;
}

.comment-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 12px;
}

.comment-content {
  flex: 1;
}

.comment-header {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}

.comment-user {
  font-weight: bold;
  margin-right: 8px;
}

.comment-time {
  font-size: 12px;
  color: #888;
}

.comment-text {
  line-height: 1.5;
  margin-bottom: 8px;
}

.comment-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.like-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: #666;
  font-size: 14px;
  cursor: pointer;
}

.like-icon {
  width: 16px;
  height: 16px;
}

.reply-btn {
  background: none;
  border: none;
  color: #666;
  font-size: 14px;
  cursor: pointer;
}

.comment-input-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background-color: white;
  border-top: 1px solid #eee;
}

.comment-input {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid #ddd;
  border-radius: 24px;
  font-size: 14px;
}

.send-btn {
  margin-left: 12px;
  padding: 8px 16px;
  background-color: var(--color-primary);
  color: white;
  border: none;
  border-radius: 24px;
  font-size: 14px;
  cursor: pointer;
}
</style>